<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${mode == 'edit' ? '编辑文章' : '新增文章'}">文章表单</title>
    <style>
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f9f9f9;
            margin: 0;
            padding: 0;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 600px;
            margin: 60px auto;
            padding: 30px;
            background: white;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
            border-radius: 12px;
            transition: transform 0.3s ease;
        }

        .container:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0,0,0,0.1);
        }

        h2 {
            text-align: center;
            color: #3a7d44;
            font-size: 24px;
            margin-bottom: 30px;
            font-weight: 600;
        }

        form {
            display: flex;
            flex-direction: column;
        }

        .form-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
            font-size: 15px;
        }

        input[type="text"],
        textarea,
        select {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 15px;
            transition: border 0.3s, box-shadow 0.3s;
        }

        input[type="text"]:focus,
        textarea:focus,
        select:focus {
            border-color: #4caf50;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
            outline: none;
        }

        textarea {
            min-height: 200px;
            resize: vertical;
        }

        .button-group {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }

        button {
            padding: 12px 25px;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .save-button {
            background-color: #4caf50;
            color: white;
            border: none;
        }

        .save-button:hover {
            background-color: #388e3c;
            transform: translateY(-2px);
        }

        .publish-button {
            background-color: #2196f3;
            color: white;
            border: none;
        }

        .publish-button:hover {
            background-color: #1976d2;
            transform: translateY(-2px);
        }

        .hidden {
            display: none;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                margin: 30px 15px;
                padding: 20px;
            }

            .button-group {
                flex-direction: column;
                gap: 10px;
            }

            button {
                width: 100%;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h2 th:text="${mode == 'edit' ? '编辑文章' : '新增文章'}">欢迎来到博客创作页面</h2>
    <form id="articleForm">
        <input type="hidden" id="articleId" name="id" th:value="${article?.id}" />
        <div class="form-group">
            <label for="title">标题:</label>
            <input type="text" id="title" name="title" th:value="${article?.title}" required>
        </div>
        <div class="form-group">
            <label for="content">内容:</label>
            <textarea id="content" name="content" rows="10" required th:text="${article?.content}"></textarea>
        </div>
        <div class="form-group">
            <label for="categoryId">分类:</label>
            <select id="categoryId" name="categoryId">
                <option value="1" th:selected="${article?.categoryId == 1}">技术</option>
                <option value="2" th:selected="${article?.categoryId == 2}">生活</option>
            </select>
        </div>
        <div class="button-group">
            <button type="button" class="save-button" onclick="saveArticle(0)">保存</button>
            <button type="button" class="publish-button" onclick="saveArticle(1)">发布</button>
        </div>
    </form>
</div>

<script>
    // 动态加载文章数据（仅在编辑模式下）
    const mode = document.querySelector('h2').innerText.includes('编辑') ? 'edit' : 'create';
    if (mode === 'edit') {
        const articleId = document.getElementById('articleId').value;
        fetch(`/article/edit/${articleId}`)
            .then(response => response.json())
            .then(data => {
                document.getElementById('title').value = data.title;
                document.getElementById('content').value = data.content;
                document.getElementById('categoryId').value = data.categoryId;
            })
            .catch(error => console.error('Error loading article data:', error));
    }

    function saveArticle(isApproved) {
        const title = document.getElementById('title').value;
        const content = document.getElementById('content').value;
        const articleId = document.getElementById('articleId').value;
        const categoryId = document.getElementById('categoryId').value;

        const articleData = {
            id: articleId || null, // 新增时 id 为 null
            title: title,
            content: content,
            categoryId: categoryId,
            isApproved: isApproved
        };

        const url = '/article/edit'; // 统一使用 /article/edit 接口
        const method = mode === 'edit' ? 'POST' : 'PUT'; // 编辑用 POST，新增用 PUT

        fetch(url, {
            method: method,
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(articleData)
        })
            .then(response => response.text())
            .then(data => {
                console.log(data);
                window.location.href = '/user/info';
            })
            .catch(error => console.error('Error:', error));
    }
</script>

</body>
</html>